home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)P / (A)P1.ADF / Polygon / palette.c < prev    next >
C/C++ Source or Header  |  1987-05-25  |  13KB  |  360 lines

  1. /***********************************************************************
  2. *  palette.c
  3. *     This program is useful for designing color palettes.
  4. * It may be used stand-alone (I would be setting the 'default map',
  5. * if preferences had a complete color map...), or you can use it
  6. * as a subroutine with minor modifications.
  7. *     One of the unique features of this program is that it prints
  8. * the hex value of the selected color, so you use it to generate
  9. * numeric values for use in programs.
  10. *     (c) 1985 by Charlie Heath of Microsmiths, this program is in
  11. * the public domain.  Not to be used commercially without permission
  12. * from MicroSmiths.
  13. ***********************************************************************/
  14.  
  15. #define I_REV   29
  16. #define G_REV   29
  17.  
  18. #define FAST register      /* Register type variable  */
  19.  
  20. #define DPTH 5       /* Raster dimensions */
  21. #define WDTH 320
  22. #define HGHT 200
  23.  
  24. #define BCOL 1          /* Background color for menus, etc   */
  25.  
  26. #define CTSIZ  32       /* In spite of myself, color table size   */
  27.  
  28. /****    Variables Initialized by init_colors()  **********/
  29. struct ColorMap *p_Co;
  30. USHORT *p_ct;           /* Color table as words */
  31.  
  32. USHORT default_color[CTSIZ];     /* Save a copy of the defaults   */
  33.  
  34.  
  35. init_colors()
  36. {
  37. FAST int i;
  38.  
  39.    p_Co = (struct ColorMap *)GetColorMap(CTSIZ);
  40.    p_ct = (USHORT *)p_Co->ColorTable;
  41.  
  42.    for ( i=0; i<CTSIZ; i++)
  43.       default_color[i] = p_ct[i];
  44.    default_color[0] = 0x20f; default_color[1] = 0xfff;
  45.    default_color[2] = 0x000; default_color[3] = 0xe30;
  46.    set_defmap();
  47. }
  48.  
  49. clear_colors()
  50. {
  51.    FreeColorMap(p_Co);
  52. }
  53.  
  54.  
  55. /****************************************************************
  56. * set_defmap()
  57. *    This routine sets up a 'default' color map.  It assumes
  58. * you have initialized the variable p_ct to point to a color
  59. * table, and that default_color has the colors you want.       */
  60. set_defmap()
  61. {
  62. FAST int i;
  63.  
  64.    for (i=0; i<CTSIZ ; i++)
  65.       *(p_ct+i) = default_color[i];
  66.  
  67.    set_cmap();
  68. }
  69.  
  70.  
  71.  
  72. /****************************************************************
  73. * set_cmap()
  74. *     This routine loads an updated p_ct color table into the
  75. * viewport.  You must have initialized the variables p_ct and
  76. * vp to point at color table and ViewPort                     */
  77. set_cmap()
  78. {
  79.    LoadRGB4(vp,p_ct,CTSIZ);
  80. }
  81.  
  82.  
  83. /***   palette modifier routines follow    ************************/
  84.  
  85. #define PLX 200      /* Palette Window Size  */
  86. #define PLY 132
  87.  
  88. #define PGAD 0             /* You can offset the gadgets here  */
  89. #define PGHI   PGAD+CTSIZ  /* Offset frm color selectors       */
  90.  
  91. struct IntuiText rtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"R",NL};
  92. struct IntuiText gtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"G",NL};
  93. struct IntuiText btxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"B",NL};
  94.  
  95. struct Image    r_img, g_img, b_img;
  96. struct PropInfo r_prop,g_prop,b_prop;
  97.  
  98. struct Gadget blue_gad = {
  99.    NL, 17,112, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  100.    PROPGADGET,(APTR)&b_img, NL,
  101.    &btxt, NL,(APTR)&b_prop, PGHI+3, NL };
  102.  
  103. struct Gadget green_gad = {
  104.    &blue_gad, 17,97, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  105.    PROPGADGET,(APTR)&g_img, NL,
  106.    >xt, NL,(APTR)&g_prop, PGHI+4, NL };
  107.  
  108. struct Gadget red_gad = {
  109.    &green_gad, 17,82, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  110.    PROPGADGET,(APTR)&r_img, NL,
  111.    &rtxt, NL,(APTR)&r_prop, PGHI+5, NL };
  112.  
  113. struct IntuiText oktxt = {3,2,JAM2,8,2,NL,(UBYTE *)" OK ",NL};
  114. struct IntuiText cntxt = {3,2,JAM2,0,2,NL,(UBYTE *)"Cancel",NL};
  115. struct IntuiText retxt = {3,2,JAM2,4,2,NL,(UBYTE *)"Reset",NL};
  116.  
  117. struct IntuiText mstxt={2,2,JAM1,0,0,NL,(UBYTE *)"MicroSmiths Palette",NL};
  118.  
  119. char frog[] = "hex";
  120. struct IntuiText hxtxt = {3,2,JAM2,136,31,NL,(UBYTE *)frog,NL};
  121.  
  122. struct Gadget re_gad = {
  123.    &red_gad, 110,112, 54,11, GADGHCOMP, RELVERIFY,
  124.    BOOLGADGET, NL, NL,
  125.    &retxt, NL,NL, PGHI+2, NL };
  126. struct Gadget cn_gad = {
  127.    &re_gad, 110,97, 54,11, GADGHCOMP, RELVERIFY,
  128.    BOOLGADGET, NL, NL,
  129.    &cntxt, NL,NL, PGHI+1, NL };
  130. struct Gadget ok_gad = {
  131.    &cn_gad, 110,82, 54,11, GADGHCOMP, RELVERIFY,
  132.    BOOLGADGET, NL, NL,
  133.    &oktxt, NL,NL, PGHI, NL };
  134.  
  135. struct Image m3C[CTSIZ] = {
  136.    {0,0,16,16,1, NL ,0,0,NL },   /* Colors   */
  137.    {0,0,16,16,1, NL ,0,1,NL },
  138.    {0,0,16,16,1, NL ,0,2,NL },
  139.    {0,0,16,16,1, NL ,0,3,NL },
  140.    {0,0,16,16,1, NL ,0,4,NL },
  141.    {0,0,16,16,1, NL ,0,5,NL },
  142.    {0,0,16,16,1, NL ,0,6,NL },
  143.    {0,0,16,16,1, NL ,0,7,NL },
  144.    {0,0,16,16,1, NL ,0,8,NL },
  145.    {0,0,16,16,1, NL ,0,9,NL },
  146.    {0,0,16,16,1, NL ,0,10,NL },
  147.    {0,0,16,16,1, NL ,0,11,NL },
  148.    {0,0,16,16,1, NL ,0,12,NL },
  149.    {0,0,16,16,1, NL ,0,13,NL },
  150.    {0,0,16,16,1, NL ,0,14,NL },
  151.    {0,0,16,16,1, NL ,0,15,NL },
  152.    {0,0,16,16,1, NL ,0,16,NL },
  153.    {0,0,16,16,1, NL ,0,17,NL },
  154.    {0,0,16,16,1, NL ,0,18,NL },
  155.    {0,0,16,16,1, NL ,0,19,NL },
  156.    {0,0,16,16,1, NL ,0,20,NL },
  157.    {0,0,16,16,1, NL ,0,21,NL },
  158.    {0,0,16,16,1, NL ,0,22,NL },
  159.    {0,0,16,16,1, NL ,0,23,NL },
  160.    {0,0,16,16,1, NL ,0,24,NL },
  161.    {0,0,16,16,1, NL ,0,25,NL },
  162.    {0,0,16,16,1, NL ,0,26,NL },
  163.    {0,0,16,16,1, NL ,0,27,NL },
  164.    {0,0,16,16,1, NL ,0,28,NL },
  165.    {0,0,16,16,1, NL ,0,29,NL },
  166.    {0,0,16,16,1, NL ,0,30,NL },
  167.    {0,0,16,16,1, NL ,0,31,NL } };
  168.  
  169. struct Gadget palg[CTSIZ] = {
  170.    { &palg[1], 3, 3,  16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  171.       BOOLGADGET,  (APTR)&m3C[0], NL,NL,NL,NL, PGAD+0, NL},
  172.    { &palg[2], 19,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  173.       BOOLGADGET,  (APTR)&m3C[1], NL,NL,NL,NL, PGAD+1, NL},
  174.    { &palg[3], 35,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  175.       BOOLGADGET,  (APTR)&m3C[2], NL,NL,NL,NL, PGAD+2, NL},
  176.    { &palg[4], 51,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  177.       BOOLGADGET,  (APTR)&m3C[3], NL,NL,NL,NL, PGAD+3, NL},
  178.    { &palg[5], 67,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  179.       BOOLGADGET,  (APTR)&m3C[4], NL,NL,NL,NL, PGAD+4, NL},
  180.    { &palg[6], 83,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  181.       BOOLGADGET,  (APTR)&m3C[5], NL,NL,NL,NL, PGAD+5, NL},
  182.    { &palg[7], 99,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  183.       BOOLGADGET,  (APTR)&m3C[6], NL,NL,NL,NL, PGAD+6, NL},
  184.    { &palg[8],115,3,  16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  185.       BOOLGADGET,  (APTR)&m3C[7], NL,NL,NL,NL, PGAD+7, NL},
  186.    { &palg[9], 3,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  187.       BOOLGADGET,  (APTR)&m3C[8], NL,NL,NL,NL, PGAD+8, NL},
  188.    { &palg[10],19,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  189.       BOOLGADGET,  (APTR)&m3C[9], NL,NL,NL,NL, PGAD+9, NL},
  190.    { &palg[11],35,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  191.       BOOLGADGET,  (APTR)&m3C[10], NL,NL,NL,NL, PGAD+10, NL},
  192.    { &palg[12],51,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  193.       BOOLGADGET,  (APTR)&m3C[11], NL,NL,NL,NL, PGAD+11, NL},
  194.    { &palg[13],67,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  195.       BOOLGADGET,  (APTR)&m3C[12], NL,NL,NL,NL, PGAD+12, NL},
  196.    { &palg[14],83,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  197.       BOOLGADGET,  (APTR)&m3C[13], NL,NL,NL,NL, PGAD+13, NL},
  198.    { &palg[15],99,19,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  199.       BOOLGADGET,  (APTR)&m3C[14], NL,NL,NL,NL, PGAD+14, NL},
  200.    { &palg[16],115,19,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  201.       BOOLGADGET,  (APTR)&m3C[15], NL,NL,NL,NL, PGAD+15, NL},
  202.    { &palg[17], 3,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  203.       BOOLGADGET, (APTR)&m3C[16], NL,NL,NL,NL, PGAD+16, NL},
  204.    { &palg[18],19,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  205.       BOOLGADGET,  (APTR)&m3C[17], NL,NL,NL,NL, PGAD+17, NL},
  206.    { &palg[19],35,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  207.       BOOLGADGET,  (APTR)&m3C[18], NL,NL,NL,NL, PGAD+18, NL},
  208.    { &palg[20],51,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  209.       BOOLGADGET,  (APTR)&m3C[19], NL,NL,NL,NL, PGAD+19, NL},
  210.    { &palg[21],67,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  211.       BOOLGADGET,  (APTR)&m3C[20], NL,NL,NL,NL, PGAD+20, NL},
  212.    { &palg[22],83,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  213.       BOOLGADGET,  (APTR)&m3C[21], NL,NL,NL,NL, PGAD+21, NL},
  214.    { &palg[23],99,35,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  215.       BOOLGADGET,  (APTR)&m3C[22], NL,NL,NL,NL, PGAD+22, NL},
  216.    { &palg[24],115,35,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  217.       BOOLGADGET,  (APTR)&m3C[23], NL,NL,NL,NL, PGAD+23, NL},
  218.    { &palg[25], 3,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  219.       BOOLGADGET, (APTR)&m3C[24], NL,NL,NL,NL, PGAD+24, NL},
  220.    { &palg[26],19,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  221.       BOOLGADGET,  (APTR)&m3C[25], NL,NL,NL,NL, PGAD+25, NL},
  222.    { &palg[27],35,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  223.       BOOLGADGET,  (APTR)&m3C[26], NL,NL,NL,NL, PGAD+26, NL},
  224.    { &palg[28],51,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  225.       BOOLGADGET,  (APTR)&m3C[27], NL,NL,NL,NL, PGAD+27, NL},
  226.    { &palg[29],67,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  227.       BOOLGADGET,  (APTR)&m3C[28], NL,NL,NL,NL, PGAD+28, NL},
  228.    { &palg[30],83,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  229.       BOOLGADGET,  (APTR)&m3C[29], NL,NL,NL,NL, PGAD+29, NL},
  230.    { &palg[31],99,51,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  231.       BOOLGADGET,  (APTR)&m3C[30], NL,NL,NL,NL, PGAD+30, NL},
  232.    { &ok_gad, 115,51,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  233.       BOOLGADGET,  (APTR)&m3C[31], NL,NL,NL,NL, PGAD+31, NL} };
  234.  
  235.  
  236. /* Used to open a Window   */
  237. struct NewWindow NewCmod = {
  238.    40,30, PLX,PLY, 2,BCOL, NL,    /* IDCMP set up AFTER CALL */
  239.    ACTIVATE | SMART_REFRESH,
  240.    &palg[0],NL, NL,    /* FirstGadget, CheckMark, Title  */
  241.    NL,NL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
  242.    PLX,PLY,PLX,PLY, CUSTOMSCREEN }; /* MinW, MinH, MaxW, MaxH */
  243.  
  244.  
  245. /********************************************************************
  246. * palette(window)
  247. *    This is the meat. This routine opens the palette window and
  248. * retains control until the user selects the OK or CANCEL gadget.
  249. *     The calling arguement is a window pointer.  That window
  250. * is expected to have opened an IDCMP port, which palette uses.
  251. ********************************************************************/
  252. palette(calling_window)
  253. struct Window *calling_window;
  254. {
  255. struct Window *cW;      /* Palette window handle   */
  256.  
  257. FAST struct IntuiMessage *imsg;
  258. FAST struct Gadget *igad;
  259.  
  260. FAST int class,cursel;
  261. BOOL keepon,munge;
  262.  
  263. USHORT backup[CTSIZ];         /* This table restores calling colors... */
  264.  
  265. static char hxtab[16] = { '0','1','2','3','4','5','6','7','8',
  266.    '9','a','b','c','d','e','f' };
  267.  
  268.    for ( cursel=CTSIZ-1; cursel >= 0; cursel--)
  269.       backup[cursel] = p_ct[cursel];
  270.  
  271.    r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  272.    r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  273.  
  274.    /* Get screen from calling window   */
  275.    NewCmod.Screen = calling_window->WScreen;  /* NEED TO SET SCREEN!!! */
  276.    if ( ! (cW = (struct Window *)OpenWindow(&NewCmod)) ) {
  277.       return(FALSE); /* Oops...  */
  278.       }
  279.  
  280.    PrintIText(cW->RPort,&mstxt,7,71);
  281.  
  282.    cW->UserPort = calling_window->UserPort;
  283.    ModifyIDCMP(cW, GADGETUP | GADGETDOWN | MENUVERIFY );
  284.  
  285.    for ( munge = keepon = TRUE; keepon; ) {
  286.  
  287.       if ( munge ) {
  288.          SetAPen(cW->RPort,cursel);
  289.          RectFill(cW->RPort,133,3,PLX-5,66);
  290.  
  291.          /* RJ will proably cringe if he see's this, but it is proably */
  292.          /* safe to modify the HorizPot values this way, UNLESS the    */
  293.          /* gadgets were being fiddled with when you do it. Here that  */
  294.          /* is quite unlikely, since another gadget was just activated */
  295.          /* to cause the munge flag to be set...                       */
  296.  
  297.          r_prop.HorizPot = (p_ct[cursel] & 0xf00) << 4;
  298.          g_prop.HorizPot = (p_ct[cursel] & 0x0f0) << 8;
  299.          b_prop.HorizPot = p_ct[cursel] << 12;
  300.          RefreshGadgets(&red_gad,cW,NL);
  301.          munge = FALSE;
  302.          hxtxt.FrontPen = cursel ^ 15;
  303.          hxtxt.BackPen  = cursel;
  304.          }
  305.  
  306.       while ( ! (imsg=(struct IntuiMessage *)GetMsg(cW->UserPort)) ) {
  307.          class = p_ct[cursel] = ((r_prop.HorizPot >> 4) & 0xf00) +
  308.             ((g_prop.HorizPot >> 8) & 0xf0) + (b_prop.HorizPot >>12);
  309.          set_cmap();
  310.  
  311.          frog[0] = hxtab[class >> 8];
  312.          frog[1] = hxtab[ (class >> 4) & 0x0f];
  313.          frog[2] = hxtab[class & 0x0f];
  314.          PrintIText(cW->RPort,&hxtxt,0,0);
  315.  
  316.          /* Proably, should do a WaitPort(cW->UserPort); */
  317.          /* I didn't, so the color updates faster.       */
  318.          /* In a multitasking environment, the system is */
  319.          /* being gronked...                             */
  320.          }
  321.  
  322.       igad =(struct Gadget *) imsg->IAddress;
  323.       if ( (class = imsg->Class) == MENUVERIFY ) {
  324.          imsg->Code = MENUCANCEL;      /* Don't Let Em Out! */
  325.          DisplayBeep(NL);
  326.          }
  327.       ReplyMsg(imsg);
  328.  
  329.       switch ( class ) {
  330.          case GADGETUP:
  331.          case GADGETDOWN:
  332.             if ((igad->GadgetID >= PGAD)&&(igad->GadgetID < PGAD+CTSIZ)) {
  333.                cursel = igad->GadgetID - PGAD;
  334.                munge = TRUE;
  335.                }
  336.             else switch ( igad->GadgetID ) {
  337.                case PGHI+1:      /* Cancel   */
  338.                   for ( class =0 ; class<CTSIZ; class++)
  339.                      p_ct[class] = backup[class];
  340.                   set_cmap();
  341.                case PGHI:        /* OK */
  342.                   keepon = FALSE;
  343.                   break;
  344.  
  345.                case PGHI+2:      /* Reset */
  346.                   set_defmap();
  347.                   munge = TRUE;
  348.                   break;
  349.                }
  350.          }
  351.       }
  352.  
  353.    cW->UserPort = NL;
  354.    CloseWindow(cW);
  355.    return(TRUE);
  356. }
  357.  
  358.  
  359. /*************************************************************/
  360.